using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.FacManage.Dto;
using ZR.Model.FacManage;
using ZR.Repository;
using ZR.Service.FacManage.IFacManageService;
using Infrastructure;
using Newtonsoft.Json.Linq;

namespace ZR.Service.FacManage
{
    /// <summary>
    /// 产线管理Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IFacLineService), ServiceLifetime = LifeTime.Transient)]
    public class FacLineService : BaseService<FacLine>, IFacLineService
    {
        /// <summary>
        /// 查询产线管理列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<FacLineDto> GetPageList(FacLineQueryDto parm)
        {
            var predicate = QueryExp(parm);
            var response = new PagedInfo<FacLineDto>();
            var total = 0;
            response.PageSize = parm.PageSize;
            response.PageIndex = parm.PageNum;
            response.Result = Queryable().Where(predicate.ToExpression())
                .LeftJoin<FacWorkshop>((line, wo) => line.WorkshopId == wo.WorkshopId)
                .LeftJoin<FacFactory>((line, wo, tory) => wo.FactoryId == tory.FactoryId)
                .OrderByPropertyNameIF(parm.Sort.IsNotEmpty(), parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc)
                .Select((line, wo, tory) => new FacLineDto
                {
                    LineId = line.LineId,
                    LineCode = line.LineCode,
                    LineName = line.LineName,
                    Remark = line.Remark,
                    CreateBy = line.CreateBy,
                    CreateTime = line.CreateTime,
                    UpdateBy = line.UpdateBy,
                    UpdateTime = line.UpdateTime,
                    FactoryInfo = tory.FactoryCode + "  " + tory.FactoryName,
                    WorkshopId = wo.WorkshopId,
                    WorkshopInfo = wo.WorkshopCode + "  " + wo.WorkshopName,
                }).ToPageList(parm.PageNum, parm.PageSize, ref total);
            response.TotalNum = total;
            return response;
        }

        /// <summary>
        /// 获取产线树信息
        /// </summary>
        /// <param name="disabledFactory">禁用工厂选择</param>
        /// <param name="disabledWorkshop">禁用车间选择</param>
        /// <returns></returns>
        public List<TreeSelectInfo> GetTreeList(bool disabledFactory, bool disabledWorkshop)
        {
            List<TreeSelectInfo> trees = new List<TreeSelectInfo>();
            //获取工厂信息
            List<FacFactory> factoryList = this.Context.Queryable<FacFactory>().ToList();
            //获取车间信息
            List<FacWorkshop> workshopList = this.Context.Queryable<FacWorkshop>().ToList();
            //获取产线信息
            List<FacLine> lineList = this.Context.Queryable<FacLine>().ToList();
            foreach (FacFactory fac in factoryList)
            {
                TreeSelectInfo tree = new TreeSelectInfo();
                tree.value = fac.FactoryId;
                tree.label = fac.FactoryCode + " " + fac.FactoryName;
                tree.disabled = disabledFactory;
                foreach (FacWorkshop wo in workshopList.Where(it => it.FactoryId == fac.FactoryId).ToList())
                {
                    TreeSelectInfo woTree = new TreeSelectInfo();
                    woTree.value = wo.WorkshopId;
                    woTree.label = wo.WorkshopCode + " " + wo.WorkshopName;
                    woTree.disabled = disabledWorkshop;
                    foreach (FacLine line in lineList.Where(it => it.WorkshopId == wo.WorkshopId).ToList())
                    {
                        woTree.children.Add(new TreeSelectInfo()
                        {
                            value = line.LineId,
                            label = line.LineCode + " " + line.LineName
                        });
                    }
                    tree.children.Add(woTree);
                }
                trees.Add(tree);
            }
            return trees;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="LineId"></param>
        /// <returns></returns>
        public FacLine GetInfo(string LineId)
        {
            var response = Queryable()
                .Where(x => x.LineId == LineId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加产线管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public FacLine AddFacLine(FacLine model)
        {
            if (this.Any(it => it.LineCode == model.LineCode))
            {
                throw new CustomException($"系统中已包含{model.LineCode}编码信息");
            }
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改产线管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateFacLine(FacLine model)
        {
            if (this.Any(it => it.LineCode == model.LineCode && it.LineId != model.LineId))
            {
                throw new CustomException($"系统中已包含{model.LineCode}编码信息");
            }
            return Update(model, true);
        }

        /// <summary>
        /// 删除产线信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteFacLine(string[] ids)
        {
            if (this.Context.Queryable<FacStation>().Any(it => ids.Contains(it.LineId)))
            {
                throw new CustomException($"系统中已经存在此产线的工位信息，不能删除");
            }
            return Delete(ids);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<FacLine> QueryExp(FacLineQueryDto parm)
        {
            var predicate = Expressionable.Create<FacLine>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LineCode), it => it.LineCode == parm.LineCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LineName), it => it.LineName.Contains(parm.LineName));
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.WorkshopId), it => it.WorkshopId == parm.WorkshopId);
            return predicate;
        }
    }
}